
import java.util.Date;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Edvin
 */
public class Macro extends Thread{
	public static void main(String[] args){
		if(args.length<4){
			pr("Usage: Macro <IP> <PORT> <START MAP NUMBER> <END MAP> [graphical]");
		}else{
			long[] l = new long[Math.abs(Integer.parseInt(args[2]) - Integer.parseInt(args[3]))+1];
			pr("macro...");
			String[] a = new String[args.length+1];
			System.arraycopy(args, 0, a, 0, args.length);
			a[args.length] = "";
			Macro macro = new Macro(true, a, l, 0, Integer.parseInt(args[2]), new boolean[l.length]);
		}
	}

	@Override
	public void run() {
		if(index>=time.length){
			pr("\nMACRO DONE");
			pr("Stats:");
			int solvedMaps = 0;
			int unsolvedMaps = 0;
			int totalTime = 0;
			int correctSolutions = 0;
			int wrongSolutions = 0;
			int timeouts = 0;
			for(int i = 0; i < time.length; i++){
				int map = i+startMap;
				if(time[i]>=0){
					totalTime+=time[i];
					if(isCorrect[i]){
						solvedMaps++;
					}else{
						unsolvedMaps++;
					}
				}else{
					timeouts++;
					unsolvedMaps++;
				}
				if(isCorrect[i]){
					correctSolutions++;
				}else if(time[i]>=0){
					wrongSolutions++;
				}
				pr("Map["+map+"]	"+(time[i]>=0?"Time: "+time[i]:"TIMEOUT")+"	"+(isCorrect[i]?"CORRECT SOLUTION":"WRONG SOLUTION"));
			}
			pr("\n\nFinal report:");
			pr("SOLVED MAPS: "+solvedMaps);
			pr("UNSOLVED MAPS: "+unsolvedMaps);
			pr("\nAdditional information:");
			pr("Total time for solved maps: "+totalTime+"	+ Timeouts: "+timeouts);
			pr("correct solutions: "+correctSolutions);
			pr("wrong solutions: "+wrongSolutions);
			System.exit(0);
		}else if(isMain){
			try{
				Date d = new Date();
				pr("MAP: "+args[2]);
				Macro m = new Macro(false, args, time, index, startMap, isCorrect);
				synchronized(m){
					m.wait(60000);
					if(m.isAlive()){
						m.interrupt();
						m.stop();
						time[index] = -1;
						pr("TIMEOUT!");
					}else{
						time[index] = new Date().getTime()-d.getTime();
						if(args[args.length-1].contains("CORRECT SOLUTION")){
							isCorrect[index] = true;
						}else{
							isCorrect[index] = false;
						}
						pr("OK!");
					}
				}
			}catch(InterruptedException e){
				pr("interupted");
			}catch(IllegalMonitorStateException e){
				time[index] = 0;
				pr("Timeout!");
			}catch(ThreadDeath e){
			}
			// start next map
			args[2] = ""+(int)(Integer.parseInt(args[2])+1);
//			pr("MAP: "+args[2]);
			Macro macro = new Macro(true, args, time, index + 1, startMap, isCorrect);
		}else{
			Client.main(args);
		}
	}
	String[] args;
	boolean isMain = true;
	long[] time;
	boolean[] isCorrect;
	int index = 0;
	int startMap = 0;
	
	public Macro(boolean isMain, String[] args, long[] time, int index, int startMap, boolean[] isCorrect) {
		this.isMain = isMain;
		this.args = args;
		this.time = time;
		this.isCorrect = isCorrect;
		this.index = index;
		this.startMap = startMap;
		start();
	}

	static void pr(Object msg){
		System.out.println(""+msg);
	}
}
